package data.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import exception.VideoNotFoundException;

import video.local.VideoBasicMethodsSet;
/**
 * 
 * @author ladjzero
 *
 */
public class ClusterTable {
	private DatabaseConnection dc;
	private String stereoTableName;
	private String clusterTableName;
	private double txtThreshold;
	
	public static class ClusterTableRecord{
		public String id;
		public String id1;
		public String id2;
		public double txtsimi;
		public double videosimi;
	}
	
	public ClusterTable(DatabaseConnection dc, String stereoTableName, String clusterTableName, double txtThreshold){
		this.dc = dc;
		this.stereoTableName = stereoTableName;
		this.clusterTableName = clusterTableName;
		this.txtThreshold = txtThreshold;
	}
	
	/**
	 * Move records from stereo table to cluster table by judging text similarity.</br>
	 * Run once, all records in stereo table will be judged.
	 * @author ladjzero
	 */
	public void addData(){
		ArrayList<ClusterTableRecord> records = new StereoTable(dc, stereoTableName).textCluster(txtThreshold);
		
		for(int i=0; i<records.size(); i++){
			String query = "INSERT INTO "+clusterTableName+" (id1, id2, txtsimi) VALUES ("+records.get(i).id1+", "+records.get(i).id2+", "+records.get(i).txtsimi+")";
			dc.updataOneRecord(query);
		}
	}
	
	//codes below is about video content cluster
	//want to find something about records dump? stop here
	/**
	 * Analyzed two videos already, submit the similarity between them to database.
	 * @param id
	 * record id in cluster table
	 * @param videosSimilarity
	 */
	public void submintVideosSimilartiy(String id, double videosSimilarity){
		String query = "UPDATE "+clusterTableName+" SET videosimi="+videosSimilarity+" WHERE id="+id;
		dc.updataOneRecord(query);
	}
	
	/**
	 * get a fresh record to analyze
	 * @param stereoTableName
	 * @return
	 */
	public String[] getVideoPairPathNoVideosimi(String stereoTableName, ClusterTableRecord record){
		String[] pathes = new String[2];
		String[] urls = getVideoPairUrlsNoVideosimi(stereoTableName, record);
		
		try {
			pathes[0] = VideoBasicMethodsSet.getVideoPathByVideoPageUrl(urls[0]);
			pathes[1] = VideoBasicMethodsSet.getVideoPathByVideoPageUrl(urls[1]);
			return pathes;
		} catch (VideoNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public String[] getVideoPairUrlsNoVideosimi(String stereoTableName, ClusterTableRecord record){
		String[] urls = new String[2];
		StereoTable st = new StereoTable(dc, stereoTableName);
		
		urls[0] = st.getUrlById(record.id1);
		urls[1] = st.getUrlById(record.id2);
		
		return urls;
		
	}
	public ClusterTableRecord getOneRecordNoVideosimi(){
		ResultSet rs;
		ClusterTableRecord record = new ClusterTableRecord();
		String query = "SELECT * FROM "+clusterTableName+" WHERE videosimi=0 LIMIT 1";
		rs = dc.executeQuery(query);
		try {
			while(rs.next()){
				record.id = rs.getString("id");
				record.id1 = rs.getString("id1");
				record.id2 = rs.getString("id2");
				record.txtsimi = rs.getDouble("txtsimi");
				
				return record;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}
